//
//  AppDelegate.m
//  Koala_Fm
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "FoundViewController.h"
#import "ListenViewController.h"
#import "EntertainViewController.h"
#import "MyViewController.h"
#import "UMSocial.h"
#import "DataBaseManager.h"
@interface AppDelegate ()<UITableViewDelegate>
@property (nonatomic, retain)UIView *balckView;
@property (nonatomic, retain)NSMutableArray *imageArr;
@property (nonatomic, retain)UIScrollView *scorllView;
@property (nonatomic, retain)UIImageView *imageView;
@property (nonatomic, assign)BOOL  record;
@end

@implementation AppDelegate
- (void)dealloc{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     [UMSocialData setAppKey:@"56a87665e0f55a79ce0025fd"];
    
#pragma mark - 数据库
    // 创建单例对象
    DataBaseManager *dbManger = [DataBaseManager shareInstance];
    // 打开数据库
    [dbManger open];
    // 创建表
    [[DataBaseManager shareInstance]create];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // 创建VC/* Found  ***/
    FoundViewController *root = [[FoundViewController alloc]init];
    // 创建导航
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:root];
    // 创建标签控制器
    UITabBarController *taBar = [[UITabBarController alloc]init];
    // 设置tabBar按钮
    root.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"fx"] tag:1000];
    /* Listen   ***/
    ListenViewController *root2 = [[ListenViewController alloc]init];
    UINavigationController *navi2 = [[UINavigationController alloc]initWithRootViewController:root2];
    root2.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"听" image:[UIImage imageNamed:@"ting"] tag:2000];
    /* Entertain ****/
    EntertainViewController *root3 = [[EntertainViewController alloc]init];
    UINavigationController *navi3 = [[UINavigationController alloc]initWithRootViewController:root3];
    root3.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"娱乐" image:[UIImage imageNamed:@"yule"] tag:3000];
    /* My    ***/
    MyViewController *root4 = [[MyViewController alloc]init];
    UINavigationController *navi4 = [[UINavigationController alloc]initWithRootViewController:root4];
    root4.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"my"] tag:4000];
    /** taBar颜色**/
    taBar.tabBar.tintColor = [UIColor redColor];
    
    // 添加管理控制器
    taBar.viewControllers = @[navi, navi2, navi3, navi4];
    // 设置window的根视图
    self.window.rootViewController = taBar;
    [root release];
    [navi release];
    
    /** 夜间模式 **/
    [self notificaSet];
    /** 引导页 **/
    [self notiScrollView];
    
    return YES;
}
#pragma mark - 注册夜间模式注册通知
-(void)notificaSet{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addView) name:@"nightCome" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeView) name:@"dayCome" object:nil];
    self.balckView = [[UIView alloc] initWithFrame:self.window.frame];
    _balckView.backgroundColor = [UIColor blackColor];
    _balckView.alpha = 0.5;
    _balckView.userInteractionEnabled = NO;
    [_balckView release];
}
   //夜间模式方法实现
-(void)addView{
    [self.window addSubview:_balckView];
}
-(void)removeView{
    [_balckView removeFromSuperview];
}

#pragma mark - 引导图注册通知
- (void)notiScrollView{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(leadScrollViewSet) name:@"addLeadView" object:nil];
    // 图片数组
    self.imageArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 3; i++) {
        NSString *string = [NSString stringWithFormat:@"pic%ld.jpg",i];
        [_imageArr addObject:string];
    }
    // 滚动视图
    self.scorllView = [[UIScrollView alloc]initWithFrame:self.window.frame];
//    _scorllView.backgroundColor = [UIColor cyanColor];
    _scorllView.contentSize = CGSizeMake(_imageArr.count*self.window.frame.size.width, 0);
    _scorllView.pagingEnabled = YES;
    _scorllView.bounces = NO;
    _scorllView.delegate = self;
    // 添加图片
    for (NSInteger i = 0; i < _imageArr.count; i++) {
        self.imageView = [[UIImageView alloc]
    initWithFrame:CGRectMake(self.window.frame.size.width *i, 0, self.window.frame.size.width, self.window.frame.size.height)];
        _imageView.image = [UIImage imageNamed:_imageArr[i]];
        // 用户交互
        _imageView.userInteractionEnabled = YES;
        // 添加轻拍手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remove)];
        [_imageView addGestureRecognizer:tap];
        [_scorllView addSubview:_imageView];
        [_imageView release];
    }
    [self.window addSubview:_scorllView];
}
- (void)remove{
    [UIView animateWithDuration:2 animations:^{
        _scorllView.alpha = 0;
    } completion:^(BOOL finished) {
        [_scorllView removeFromSuperview];
    }];
    
}
    // 移除引导图
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == self.window.frame.size.width *(_imageArr.count - 1)) {
        [UIView animateWithDuration:2 animations:^{
            _imageView.alpha = 0.5;
            _imageView.frame = CGRectZero;
            _imageView.center = CGPointMake(self.window.frame.size.width *3 - self.window.frame.size.width / 2, self.window.frame.size.height / 2);
        }completion:^(BOOL finished) {
           
            [_scorllView removeFromSuperview];
        }];
        
        }
    
    }
#pragma mark - 引导图
- (void)leadScrollViewSet{
    // 判断第一次进入funVC 防止重复添加引导页
    _record = YES;
    if (!_record) {
        [self.window addSubview:_scorllView];
        
        
    }
    _record = !_record;
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// 本地通知回调函数，当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    // 这里真实需要处理交互的地方
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@"key"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"猜你喜欢" message:notMess delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
    // 在不需要再推送时,可以取消推送
    [MyViewController cancelLocalNotificationWithKey:@"key"];
}

@end
